//
//  HELSepcialistDetailVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/13.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELSepcialistDetailVC.h"
#import "HELCourseListModel.h"
#import "HELCourseCell.h"
#import "NHCourseDetailVC.h"
#import "HELSepcialistDtailHeaderCell.h"
#import "HELSecoalistModel.h"

@interface HELSepcialistDetailVC ()


@property (nonatomic,  strong) HELSecoalistModel *specialist;


@end

@implementation HELSepcialistDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELCourseCell" bundle:nil] forCellReuseIdentifier:@"HELCourseCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"HELSepcialistDtailHeaderCell" bundle:nil] forCellReuseIdentifier:@"HELSepcialistDtailHeaderCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 140;
    self.title =@"专家简介";
//    weakSelf_define;
//    [self.tableView  wsj_HeaderRefreshBlock:^{
//          [weakSelf getSpecialistDetail];
//    }];
    [self getSpecialistDetail];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (!self.specialist) {
        return 0;
        
    }else{
        if (section == 0) {
            return 1;
        }else{
            
            return self.specialist.relatedCourses.count;
        }
    }

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   
    
    return 2;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return  UITableViewAutomaticDimension;
    }
    
    
    return 120;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    weakSelf_define;
    if (indexPath.section == 1) {
        HELCourseCell *cell =[tableView dequeueReusableCellWithIdentifier:@"HELCourseCell"];
        cell.courseListModel = self.specialist.relatedCourses[indexPath.row];
        
        return cell;
    }else{
        HELSepcialistDtailHeaderCell *cell=[tableView dequeueReusableCellWithIdentifier:@"HELSepcialistDtailHeaderCell"];
        cell.likeBlock  = ^(){
            [weakSelf likeRequest];
           
        };
        cell.expendBlock = ^(){
          
            [weakSelf.tableView reloadData];
        };
        cell.model = self.specialist;
        return cell;
    }
  
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
            HELCourseListModel *model = self.specialist.relatedCourses[indexPath.row];
            NHCourseDetailVC *vc =[NHCourseDetailVC new];
            vc.courseId = [model.id integerValue];
            [self.navigationController pushViewController:vc animated:YES];
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
    if (section == 1) {
        return 39;
    }else{
        return 0;
    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

   
    UIView *div =[UIView new];
    div.frame = CGRectMake(0, 0, kScreenWidth, 39);
    div.backgroundColor =[UIColor whiteColor];
    
    
    UIView *line =[UIView new];
    
    line.frame =CGRectMake(20,10, 4, 20);
    line.backgroundColor = kMainColor;
    [div addSubview:line];
    
    UILabel *label =[UILabel new];
    label.text = @"相关课程";
    label.textColor = kTxtColor;
    label.font = [UIFont systemFontOfSize:16];
    label.frame = CGRectMake(40, 10, 0, 0);
    [label sizeToFit];
    [div addSubview:label];
    
    return div;
    
    

}

#pragma mark 专家详情
- (void)getSpecialistDetail{

    
   
    NSString *url = [NSString stringWithFormat:@"%@/%@",helSpecialistDetail,@(self.specialistId)];
    weakSelf_define;
    if (!weakSelf.specialist) {
         [self wsj_showLodingGif];
    }
   
    [[HELNetworkManager sharedMg] GET:url parameters:nil success:^(id responseObject) {
       [weakSelf wsj_hideLodingGif];
//        [weakSelf.tableView.header endRefreshing];
        weakSelf.specialist =  [HELSecoalistModel mj_objectWithKeyValues:responseObject[@"data"]];
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSString *err) {
        [weakSelf wsj_hideLodingGif];
//         [weakSelf.tableView.header endRefreshing];
        if (!weakSelf.specialist) {
            [weakSelf wsj_showLoadFailviewWithRetry:^{
                [weakSelf getSpecialistDetail];
            }];
        }
       
        
    }];
    
    
}

#pragma mark 关注
- (void)likeRequest{
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    
    weakSelf_define;
    BOOL isFollow = [self.specialist.isFollow integerValue];
    params[@"status"] = @(isFollow);
    params[@"lecturerId"]  =self.specialist.id;
   [[HELNetworkManager sharedMg] PUT:helFollowOrCancel parameters:params progress:nil success:^(id responseobject) {
       if (!isFollow) {
           cwqHudSuccess(@"关注成功!可以在我的专家里面查看关注专家");
       }
       weakSelf.specialist.isFollow = [@(!isFollow) stringValue];
       
   } failure:^(NSString * err) {
       
   }];
   
}




@end
